package chen.web;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;

import chen.web.event.method.MethodEventSupport;
import chen.web.forum.TopicManager;
import chen.web.user.AccountManager;
import chen.web.user.EventConfigUtils;
import chen.web.user.SecurityListener;

public class MethodEventConfig {

	
	public static MethodEventSupport newEventSupport(){		
		MethodEventSupport es = new MethodEventSupport(getAllEventTypes());		
		//es.register(new SecurityListener(new EventConfigUtils(""),null), getEventTypes(AccountManager.class));
	
		return es;
	}
	
	private static String[] getEventTypes(Class<?> clazz){
		ArrayList<String> eventTypes = new ArrayList<String>();
		Field[] fields = clazz.getFields();
		for(Field field : fields){
			String name = field.getName();
			if(name.startsWith("ME_")){
				try {
					String value = (String)field.get(clazz);
					eventTypes.add(value);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					throw new RuntimeException(e);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					throw new RuntimeException(e);
				}
			}
		}
		return eventTypes.toArray(new String[0]);
	}
	
	private static String[] getAllEventTypes(){
		ArrayList<String> eventTypes = new ArrayList<String>();
		eventTypes.addAll(Arrays.asList(getEventTypes(AccountManager.class)));
		eventTypes.addAll(Arrays.asList(getEventTypes(TopicManager.class)));
		
		return eventTypes.toArray(new String[0]);
	}
}
